$(function(){
    if(window.parent == window.self){
        max_width = window.document.documentElement.clientWidth;
        max_height = window.document.documentElement.clientHeight;
    }else{
        var panel = $(".tabs-panels",window.parent.document);

        max_width = panel.width;
        max_height = panel.height;
    }

    $('#listrole').datagrid({
        toolbar: '#toolbar',
        title: '角色列表',
        iconCls: 'icon icon-chart_organisation',
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        fit:true,
        url: '/sys/role/list',
        queryParams:{
            _token : $('meta[name="csrf-token"]').attr('content'),
            curr_menu_id : $('meta[name="curr_menu_id"]').attr('content')
        },
        columns: [[
            { title: '角色名称', field: 'name',width: 130 },
            { title: '描述', field: 'description', width: 130 },
            { title: '是否生效', field: 'is_active', width: 130 },
            { title: '操作', field: 'id', width: 130, sortable:true, formatter: function(value,rows,index){
                    if(!value == false) {
                    return '<a onclick="roleassign('+ rows.id +','+index+')" >角色分配</a>';
                    }
                }
            }
        ]],
        onLoadSuccess: function (data) {
            if (data === null || data.total == 0) {
                var body = $(this).data().datagrid.dc.body2;
                body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 35px; text-align: center;"><h1>暂无数据</h1></td></tr>');
            }
        }
    });
})
function roleassign(id,index){
        var rows = $('#listrole').datagrid('getData').rows[index];
        $('#form_dialog').dialog({
        title: '用户角色分配',
        width: 400,
        height: 200,
        closed: false,
        cache: false,
        href: '/sys/role/roleassign?role_id='+rows.id,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save1
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}
function openAdd(){
    $('#form_dialog').dialog({
        title: '添加信息',
        width: 400,
        height: 200,
        closed: false,
        cache: false,
        href: '/sys/role/add',
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function openEdit(){
    var rows = $('#listrole').datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行" , "info");
        return;
    }
    $('#form_dialog').dialog({
        title: '编辑信息',
        width: 400,
        height: 200,
        closed: false,
        cache: false,
        href: '/sys/role/edit?role_id='+rows.id,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function save(){
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:'/sys/role/save',
        type:'post',
        data:$('#info-form').serialize(),
        dataType:'json',
        success:function (data) {
            if(data.code == 200){
                $('#listrole').datagrid("reload")
                $('#form_dialog').dialog('close');
            }else{
                $.messager.alert("提示信息", data.data , "error");
            }
        }
    });
}

function save1(){
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:'/sys/role/delAdd',
        type:'post',
        data:$('#info-form').serialize(),
        dataType:'json',
        success:function (data) {
            if(data.code == 200){
                $('#listrole').datagrid("reload")
                $('#form_dialog').dialog('close');
            }else{
                $.messager.alert("提示信息", data.data , "error");
            }
        }
    });
}


function reload(){
    $('#listrole').datagrid("reload");
}
